//
//  SwitchButton.swift
//  GreenDeal
//
//  Created by Moennig Q on 8/12/2022.
//

import UIKit
import RxCocoa
import RxSwift
class SwitchButton: UIButton {
    private lazy var disposeBag = DisposeBag()
    static let size = CGSize(width: 50, height: 30)
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.rx.tap.subscribe {[weak self] _ in
            if self?.tag == 1{
                self?.switchOn(false)
            }else{
                self?.switchOn(true)
            }
        }.disposed(by: self.disposeBag)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK: - onFlag: true表示开启，false，表示关闭
    func switchOn(_ onFlag: Bool){
        let imageName = onFlag ? "scanner_setting_switch_on" : "scanner_setting_switch_off"
        self.setImage(UIImage(named: imageName), for: UIControl.State.normal)
        self.tag = onFlag ? 1 : 0
    }
    
}
